package tk.uodrad.apex.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.pojo.dto.PeopleAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.PeopleUpdateDTO;
import tk.uodrad.apex.project.pojo.vo.PeopleListVO;
import tk.uodrad.apex.project.service.PeopleService;
import tk.uodrad.apex.project.web.JsonResult;
import tk.uodrad.apex.project.web.ServiceCode;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 说明：
 */
@Slf4j
@RestController
@RequestMapping("/owners")
@Transactional
public class PeopleController {
    @Autowired
    private PeopleService peopleService;

    /**
     * 添加成员
     * @param peopleAddNewDTO 添加成员DTO
     * */
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody PeopleAddNewDTO peopleAddNewDTO) {
        peopleService.save(peopleAddNewDTO);
        return JsonResult.ok("添加成功");
    }

    /**
     * 根据ID数组删除成员
     * @param ids ID数组
     * */
    @GetMapping("/delete")
    public JsonResult<String> deleteById(Long[] ids) {
        peopleService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok("删除成功！");
    }

    /**
     * 根据ID修改成员信息
     * @param peopleUpdateDTO 修改成员DTO
     * */
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody PeopleUpdateDTO peopleUpdateDTO) {
        peopleService.updateById(peopleUpdateDTO);
        return JsonResult.ok("修改成功！");
    }

    /**
     * 分页查询成员列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<PeopleListVO> PeopleListVO分页对象
     * */
    @GetMapping("/list")
    public JsonResult<IPage<PeopleListVO>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "1000000000") Integer pageSize,
                                                @RequestParam(value = "communityCode", required = false) String communityCode) {
        IPage<PeopleListVO> list = peopleService.list(pageNum, pageSize, communityCode);
        return JsonResult.ok(list);
    }

    @GetMapping("/countPeople")
    public JsonResult<Integer> countPeople() {
        Integer countPeople = peopleService.countPeople();
        return JsonResult.ok(countPeople);
    }

    @PostMapping("/uploadExecl")
    public JsonResult<String> uploadExcel(@RequestPart("file") MultipartFile file) {
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            // 加载Excel文件
            // 获取第一个sheet
            Sheet sheet = workbook.getSheetAt(0);
            // 遍历sheet的每一行
            for (Row row : sheet) {
                // 创建一个实体对象
                PeopleAddNewDTO peopleAddNewDTO = new PeopleAddNewDTO();
                // 从每个单元格获取数据并设置到实体中
                peopleAddNewDTO.setCommunityCode(row.getCell(1).getStringCellValue());
                peopleAddNewDTO.setPropertyCode(row.getCell(2).getStringCellValue());
                peopleAddNewDTO.setPeopleName(row.getCell(3).getStringCellValue());
                peopleAddNewDTO.setIdNumber(row.getCell(4).getStringCellValue());
                peopleAddNewDTO.setTelephone(row.getCell(5).getStringCellValue());
                peopleAddNewDTO.setJob(row.getCell(6).getStringCellValue());
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                peopleAddNewDTO.setBirthday(dateFormat.parse(row.getCell(7).getStringCellValue()));
                peopleAddNewDTO.setGender((int) row.getCell(8).getNumericCellValue());
                peopleAddNewDTO.setDescribes(row.getCell(9).getStringCellValue());
                // 将实体保存到数据库
                peopleService.save(peopleAddNewDTO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(ServiceCode.ERROR, "数据导入错误，请检查Execl内容！");
        }
        return JsonResult.ok("导入成功！");
    }
}
